package com.finance.filter;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.finance.base.constant.BaseConstants;
import com.finance.framework.lang.BeanUtils;
import com.finance.system.bean.pojo.User;
import com.finance.system.service.IConfigService;
import com.finance.util.HttpUtil;
import com.finance.util.OKHTTPUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class AuthFilter implements Filter {
	protected  final Logger logger = LoggerFactory.getLogger(AuthFilter.class);

	private String backEndLoginUrl = "";
	private String authThird = "";
	private ObjectMapper objectMapper = new ObjectMapper();
	private IConfigService configService;
	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		User user = (User)request.getSession().getAttribute("authUser");
		if(user==null){
			try {
			String userStr = OKHTTPUtil.getWithJessionid(authThird+";jsessionid="+request.getParameter("backendSessionId"),request.getParameter("backendSessionId"));
			Map map = this.objectMapper.readValue(userStr,Map.class);
			Map usermap = (Map)map.get("data");
			user = new User();
			BeanUtils.populate(user,usermap);
			} catch (IllegalAccessException e) {
				logger.error(e.getMessage(),e);
			} catch (InvocationTargetException e) {
				logger.error(e.getMessage(),e);
			} catch (Exception e) {
				logger.error(e.getMessage(),e);
			}
			if(user!=null&& StringUtils.isBlank(user.getCode())){
				response.sendRedirect(backEndLoginUrl);
			}else{
				request.getSession().setAttribute("authUser",user);
				filterChain.doFilter(request, response);
			}
		}else{
			filterChain.doFilter(request, response);
		}


	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		ServletContext servletContext = filterConfig.getServletContext();
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		configService = (IConfigService) ctx.getBean("configService");
		authThird = configService.findConfig(BaseConstants.SYSCONFIG_KEY_BACKEND_BASE_URL) + "/" + configService.findConfig(BaseConstants.SYSCONFIG_KEY_BACKEND_THIRDAUTH_URL);
		backEndLoginUrl = configService.findConfig(BaseConstants.SYSCONFIG_KEY_BACKEND_BASE_URL) + "/" + configService.findConfig(BaseConstants.SYSCONFIG_KEY_BACKEND_LOGIN_URL);
	}

}
